﻿Imports DevExpress.XtraReports.UI

Public Class FormNgList
    Public usr As String
    Dim objGt As New clsGt
    Dim objGtResultDetails As New clsGtResultDetails
    Dim objGtNgReasonDetails As New clsGtNgReasonDetails
    Dim objGtProcedure As New clsGtProcedure
    Dim txt As TextBox
    Dim idList As String
    Dim dtNgDetails As DataTable
    Dim dtFan As New DataTable
    Dim dtFanNew As DataTable
    Dim dtTui As New DataTable
    Dim dtTuiNew As DataTable
    Private Sub FormNgList_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim startDate As String = Date.Now.ToShortDateString()
        txtStartDate.Text = startDate
        Dim endDate As String = Date.Now.ToShortDateString()
        txtEndDate.Text = endDate
        initDataTable()
        setDgvData()
        setDgvNgOperData()
    End Sub

    Private Sub initDataTable()
        dtFan.Columns.Add("material_type", GetType(String))
        dtFan.Columns.Add("material_name", GetType(String))
        dtFan.Columns.Add("size", GetType(String))
        dtFan.Columns.Add("count", GetType(Integer))
        dtFan.Columns.Add("reason", GetType(String))
        dtFan.Columns.Add("note", GetType(String))
        dtFanNew = dtFan.Clone()
        dtTui.Columns.Add("material_type", GetType(String))
        dtTui.Columns.Add("material_name", GetType(String))
        dtTui.Columns.Add("size", GetType(String))
        dtTui.Columns.Add("count", GetType(Integer))
        dtTui.Columns.Add("reason", GetType(String))
        dtTui.Columns.Add("into_date", GetType(String))
        dtTui.Columns.Add("assist_marker", GetType(String))
        dtTui.Columns.Add("note", GetType(String))
        dtTuiNew = dtTui.Clone()
    End Sub

    Private Sub btnSearce_Click(sender As Object, e As EventArgs) Handles btnSearce.Click
        setDgvData()
        setDgvNgOperData()
    End Sub
    Public Sub setDgvData()
        If String.IsNullOrEmpty(txtStartDate.Text.Trim()) = False And String.IsNullOrEmpty(txtEndDate.Text.Trim()) = False Then
            Dim dtGt As DataTable = objGt.getGtList(txtStartDate.Text.Trim() + " 00:00:00.000", txtEndDate.Text.Trim() + " 23:59:59.999")
            dtGt.Columns.Add("oper_type_name")
            idList = String.Empty
            If dtGt.Rows.Count > 0 Then
                For Each row As DataRow In dtGt.Rows
                    If row("oper_type") = "0" Then
                        row("oper_type_name") = "骨架板"
                    Else
                        row("oper_type_name") = "法兰板"
                    End If
                    idList &= "'" & row("sys_id") & "',"
                Next
                If idList.Length > 0 Then
                    idList = idList.Substring(0, idList.Length - 1)
                End If
            End If
            dgv.AutoGenerateColumns = False
            dgv.DataSource = dtGt
        Else
            MsgBox("请输入开始时间和结束时间")
        End If
    End Sub

    Public Sub setDgvNgOperData()
        If idList.Length > 0 Then
            dtNgDetails = objGtNgReasonDetails.getGtNgDetailsLists(idList)
            'objGtNgReasonDetails.getGtNgDetailsList("1", txtStartDate.Text.Trim() + " 00:00:00.000", txtEndDate.Text.Trim() + " 23:59:59.999")
            dtNgDetails.Columns.Add("procedure_no_name")
            If dtNgDetails.Rows.Count > 0 Then
                For Each row As DataRow In dtNgDetails.Rows
                    If row("oper_type") = "0" Then
                        If row("procedure_no") = "0" Then
                            row("procedure_no_name") = "领料"
                        End If
                        If row("procedure_no") = "1" Then
                            row("procedure_no_name") = "校平"
                        End If
                        If row("procedure_no") = "2" Then
                            row("procedure_no_name") = "喷砂清洗"
                        End If
                        If row("procedure_no") = "3" Then
                            row("procedure_no_name") = "六工位"
                        End If
                    End If
                    If row("oper_type") = "1" Then
                        If row("procedure_no") = "0" Then
                            row("procedure_no_name") = "领料"
                        End If
                        If row("procedure_no") = "1" Then
                            row("procedure_no_name") = "喷砂清洗"
                        End If
                        If row("procedure_no") = "2" Then
                            row("procedure_no_name") = "三工位"
                        End If
                    End If
                Next
                setDgv2Data()
            End If
            dgvNgOper.AutoGenerateColumns = False
            dgvNgOper.DataSource = dtNgDetails
        End If
    End Sub

    Public Sub setDgv2Data()
        If dtNgDetails.Rows.Count > 0 Then
            dtFan.Rows.Clear()
            dtTui.Rows.Clear()
            For Each row As DataRow In dtNgDetails.Rows
                If row("oper_type") = "0" Then
                    If row("procedure_no") = "3" Then
                        dtFan.Rows.Add("骨架板", row("type_value"), row("size"), row("ng_num"),
                                       row("ng_reason"), "")
                    Else
                        dtTui.Rows.Add("骨架板", row("type_value"), row("size"), row("ng_num"),
                                       row("ng_reason"), DateTime.Parse(row("into_date")).ToShortDateString(), row("assist_marker"), "")
                    End If
                End If
                If row("oper_type") = "1" Then
                    If row("procedure_no") = "2" Then
                        dtFan.Rows.Add("法兰板", row("type_value"), row("size"), row("ng_num"),
                                       row("ng_reason"), "")
                    Else
                        dtTui.Rows.Add("法兰板", row("type_value"), row("size"), row("ng_num"),
                                       row("ng_reason"), DateTime.Parse(row("into_date")).ToShortDateString(), row("assist_marker"), "")
                    End If
                End If
            Next
            dtFanNew.Rows.Clear()
            For index = 0 To dtFan.Rows.Count - 1
                Dim flt As String = "material_type= '" & dtFan.Rows(index)("material_type") & "' and material_name='" & dtFan.Rows(index)("material_name") & "' and size='" & dtFan.Rows(index)("size") & "' and reason='" & dtFan.Rows(index)("reason") & "' and note='" & dtFan.Rows(index)("note") & "'"
                Dim dtCompute = dtFan.Compute("Sum(count)", flt)
                Dim drst As DataRow() = dtFanNew.Select(flt)
                If drst.Length = 0 Then
                    dtFanNew.Rows.Add(dtFan.Rows(index)("material_type"), dtFan.Rows(index)("material_name"), dtFan.Rows(index)("size"), dtCompute.ToString(), dtFan.Rows(index)("reason"), dtFan.Rows(index)("note"))
                End If
            Next
            dgvFan.AutoGenerateColumns = False
            dgvFan.DataSource = dtFanNew

            dtTuiNew.Rows.Clear()
            For index = 0 To dtTui.Rows.Count - 1
                Dim fltt As String = "material_type= '" & dtTui.Rows(index)("material_type") & "' and material_name='" & dtTui.Rows(index)("material_name") & "' and size='" & dtTui.Rows(index)("size") & "' and reason='" & dtTui.Rows(index)("reason") & "' and into_date='" & dtTui.Rows(index)("into_date") & "' and assist_marker='" & dtTui.Rows(index)("assist_marker") & "' and note='" & dtTui.Rows(index)("note") & "'"
                Dim dtComputeT = dtTui.Compute("Sum(count)", fltt)
                Dim drstt As DataRow() = dtTuiNew.Select(fltt)
                If drstt.Length = 0 Then
                    dtTuiNew.Rows.Add(dtTui.Rows(index)("material_type"), dtTui.Rows(index)("material_name"), dtTui.Rows(index)("size"), dtComputeT.ToString(), dtTui.Rows(index)("reason"), dtTui.Rows(index)("into_date"), dtTui.Rows(index)("assist_marker"), dtTui.Rows(index)("note"))
                End If
            Next
            dgvTui.AutoGenerateColumns = False
            dgvTui.DataSource = dtTuiNew
        End If
    End Sub

    Private Sub txt_MouseClick(sender As Object, e As MouseEventArgs) Handles txtStartDate.MouseClick, txtEndDate.MouseClick
        txt = CType(sender, TextBox)
        DateNavigator1.Left = txt.Left
        DateNavigator1.Top = txt.Location.Y + txt.Height
        DateNavigator1.Visible = True
    End Sub


    Private Sub DateNavigator1_DateTimeChanged(sender As Object, e As EventArgs) Handles DateNavigator1.DateTimeChanged
        txt.Text = DateNavigator1.DateTime.ToShortDateString()
        DateNavigator1.Visible = False
    End Sub

    Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click

    End Sub

    Private Sub btnPrintFan_Click(sender As Object, e As EventArgs) Handles btnPrintFan.Click
        Dim report As New XtraReportFan()
        Dim printTool As New ReportPrintTool(report)
        If dtFanNew.Rows.Count > 0 Then
            For index = 0 To dtFanNew.Rows.Count - 1
                report.FindControl("xtc_1_" & index.ToString(), True).Text = dtFanNew.Rows(index)("material_type")
                report.FindControl("xtc_2_" & index.ToString(), True).Text = dtFanNew.Rows(index)("material_name")
                report.FindControl("xtc_3_" & index.ToString(), True).Text = dtFanNew.Rows(index)("size")
                report.FindControl("xtc_4_" & index.ToString(), True).Text = dtFanNew.Rows(index)("count")
                report.FindControl("xtc_5_" & index.ToString(), True).Text = dtFanNew.Rows(index)("reason")
                report.FindControl("xtc_6_" & index.ToString(), True).Text = dtFanNew.Rows(index)("note")
            Next
            'report.Xr_lbl_support_model.Text = dtPS.Rows(0)("prod_spec_name")
            'report.Xr_lbl_project_name.Text = dtPM.Rows(0)("mes_project_name")
            'report.Xr_lbl_project_code.Text = dtPI.Rows(0)("project_no") 'dtxxx.Rows(0)("mes_project_code") '项目编号
            'report.Xr_lbl_yzz_DateTime.Text = Now.ToString 'dtx.Rows(0)("org_time") '预组装时间
            'report.XrBarCode1.Text = dtPI.Rows(0)("prod_id") 'lblprodid.Text.Trim() '生产编号
            'report.Xr_lbl_jp_type.Text = dtPI.Rows(0)("jp_type") 'lblJLXH.Text.Trim() '胶料类型     
            'report.Xr_lbl_gyh.Text = dtPI.Rows(0)("steel_seal_no") ' dtx.Rows(0)("steel_seal_no") '钢印号
            'report.Xr_lbl_location.Text = cmbLoc.Text '库位编号
            printTool.Print()
        End If
    End Sub

    Private Sub btnPrintTui_Click(sender As Object, e As EventArgs) Handles btnPrintTui.Click
        'Dim formNgSummary As New FormNgSummary()
        'formNgSummary.ShowDialog(Me)
        Dim report As New XtraReportTui()
        Dim printTool As New ReportPrintTool(report)
        If dtTuiNew.Rows.Count > 0 Then
            For index = 0 To dtTuiNew.Rows.Count - 1
                report.FindControl("xtc_1_" & index.ToString(), True).Text = dtTuiNew.Rows(index)("material_type") & " " & dtTuiNew.Rows(index)("material_name")
                report.FindControl("xtc_3_" & index.ToString(), True).Text = dtTuiNew.Rows(index)("size")
                report.FindControl("xtc_4_" & index.ToString(), True).Text = dtTuiNew.Rows(index)("count")
                report.FindControl("xtc_5_" & index.ToString(), True).Text = dtTuiNew.Rows(index)("reason")
                report.FindControl("xtc_6_" & index.ToString(), True).Text = dtTuiNew.Rows(index)("into_date")
                report.FindControl("xtc_7_" & index.ToString(), True).Text = dtTuiNew.Rows(index)("assist_marker")
                report.FindControl("xtc_8_" & index.ToString(), True).Text = dtTuiNew.Rows(index)("note")
            Next
            'report.Xr_lbl_support_model.Text = dtPS.Rows(0)("prod_spec_name")
            'report.Xr_lbl_project_name.Text = dtPM.Rows(0)("mes_project_name")
            'report.Xr_lbl_project_code.Text = dtPI.Rows(0)("project_no") 'dtxxx.Rows(0)("mes_project_code") '项目编号
            'report.Xr_lbl_yzz_DateTime.Text = Now.ToString 'dtx.Rows(0)("org_time") '预组装时间
            'report.XrBarCode1.Text = dtPI.Rows(0)("prod_id") 'lblprodid.Text.Trim() '生产编号
            'report.Xr_lbl_jp_type.Text = dtPI.Rows(0)("jp_type") 'lblJLXH.Text.Trim() '胶料类型     
            'report.Xr_lbl_gyh.Text = dtPI.Rows(0)("steel_seal_no") ' dtx.Rows(0)("steel_seal_no") '钢印号
            'report.Xr_lbl_location.Text = cmbLoc.Text '库位编号
            printTool.Print()
        End If
    End Sub
End Class